package com.weipeep.sys.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.google.common.collect.Lists;
import com.weipeep.common.annotation.Log;
import com.weipeep.common.base.BaseController;
import com.weipeep.common.config.Constant;
import com.weipeep.common.domain.DictDO;
import com.weipeep.common.domain.Tree;
import com.weipeep.common.service.DictService;
import com.weipeep.common.utils.FileType;
import com.weipeep.common.utils.IdGenerator;
import com.weipeep.common.utils.MD5Utils;
import com.weipeep.common.utils.Result;
import com.weipeep.sys.domain.DeptDO;
import com.weipeep.sys.domain.RoleDO;
import com.weipeep.sys.domain.UserDO;
import com.weipeep.sys.domain.UserFileDO;
import com.weipeep.sys.service.RoleService;
import com.weipeep.sys.service.UserService;
import com.weipeep.sys.vo.UserFileVo;
import com.weipeep.sys.vo.UserVO;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <pre>
 * </pre>
 * 
 * <small> 2018年3月23日 | wangyun</small>
 */
@RequestMapping("/sys/user/audit")
@Controller
public class UserAuditController extends BaseController {
    private String prefix = "sys/audit";
    @Autowired
    UserService userService;
    @Autowired
    RoleService roleService;
    @Autowired
    DictService dictService;
    @Autowired
    IdGenerator idGenerator;
    @RequiresPermissions("sys:user:audit")
    @GetMapping("")
    String user(Model model) {
        return prefix + "/audit";
    }

    @GetMapping("/list")
    @ResponseBody
    public Result<Page<UserDO>> list(Integer pageNumber, Integer pageSize, UserDO userDTO) {
        // 查询列表数据
        Page<UserDO> page = new Page<>(pageNumber, pageSize);

        Wrapper<UserDO> wrapper = new EntityWrapper<UserDO>(userDTO);
        wrapper.eq("user_type",Constant.UserType.MEMBER);
        page = userService.selectPage(page, wrapper);
        int total = userService.selectCount(wrapper);
        page.setTotal(total);
        return Result.ok(page);
    }

    @RequiresPermissions("sys:user:audit:edit")
    @Log("编辑用户")
    @GetMapping("/edit/{id}")
    String edit(Model model, @PathVariable("id") Long id) {
        UserDO userDO = userService.selectById(id);
        model.addAttribute("user", userDO);
        List<RoleDO> roles = roleService.findListByUserId(id);
        List<RoleDO> rolesNeed = Lists.newArrayList();
        for (RoleDO roleDO:roles){
            if (roleDO.getRoleName().contains("买家")){
                if (userDO.getMemberType().equals(1)||userDO.getMemberType().equals(3)){
                    roleDO.setRoleSign("true");
                }
                rolesNeed.add(roleDO);
                continue;
            }
            if (roleDO.getRoleName().contains("卖家")){
                if (userDO.getMemberType().equals(2)||userDO.getMemberType().equals(3)){
                    roleDO.setRoleSign("true");
                }
                rolesNeed.add(roleDO);
                continue;
            }
        }
        model.addAttribute("roles", rolesNeed);
        return prefix + "/edit";
    }

    @Log("查看用户")
    @GetMapping("/view/{id}")
    String view(Model model, @PathVariable("id") Long id) {
        UserDO userDO = userService.selectById(id);
        List<UserFileVo> userFileVoList = userService.fetchPictures(id,null);
        model.addAttribute("user", userDO);
        model.addAttribute("userFileVoList",userFileVoList);
        return prefix + "/view";
    }
    @RequiresPermissions("sys:user:audit:edit")
    @Log("更新用户")
    @PostMapping("/update")
    @ResponseBody
    Result<String> update(UserDO user) {
        userService.updateUserForAudit(user);
        return Result.ok();
    }

    @RequiresPermissions("sys:user:audit:remove")
    @Log("删除用户")
    @PostMapping("/remove")
    @ResponseBody
    Result<String> remove(Long id) {
        userService.deleteById(id);
        return Result.ok();
    }

    @RequiresPermissions("sys:user:audit:batchRemove")
    @Log("批量删除用户")
    @PostMapping("/batchRemove")
    @ResponseBody
    Result<String> batchRemove(@RequestParam("ids[]") Long[] userIds) {
        userService.deleteBatchIds(Arrays.asList(userIds));
        return Result.ok();
    }
}
